package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @description: ValidateCodeweb层
 * @author: wangjingliang
 * @create: 2019-11-19 14:32
 **/
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机号码到后台进行验证
     * url:/validateCode/send4Order.do?telephone
     * @param:
     * @return: null
     * @date: 2019/11/19
     */
    @PostMapping("/send4Order")
    public Result send4Order(String telephone) {
        if (HealthStringUtils.isEmpty(telephone)){
            return new Result(false, "手机号码不能为空");
        }
        String validateCode4String = null;
        //生成验证码
        try {
            validateCode4String = ValidateCodeUtils.generateValidateCode4String(4);
            //发送给用户
            QuerySendDetailsUtils.sendDetails(telephone,validateCode4String);
            System.out.println(validateCode4String);
            //将用户的手机和验证码进行储存到redis中,并设置存活时间为5分钟
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        try {
            redisTemplate.opsForValue().set(telephone + RedisMessageConstant.SENDTYPE_ORDER, validateCode4String,300,TimeUnit.SECONDS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EXCEPION);
        }
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, validateCode4String);
    }


    /**
     * 发送手机号码到后台进行验证
     * url:validateCode/send4Login.do?telephone
     * @param:
     * @return: null
     * @date: 2019/11/19
     */
    @PostMapping("/send4Login")
    public Result send4Login(String telephone) {
        if (HealthStringUtils.isEmpty(telephone)){
            return new Result(false, "手机号码不能为空");
        }
        String validateCode4String = null;
        //生成验证码
        try {
            validateCode4String = ValidateCodeUtils.generateValidateCode4String(4);
            //发送给用户
            QuerySendDetailsUtils.sendDetails(telephone,validateCode4String);
            System.out.println(validateCode4String);
            //将用户的手机和验证码进行储存到redis中,并设置存活时间为5分钟
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        try {
            redisTemplate.opsForValue().set(telephone + RedisMessageConstant.SENDTYPE_LOGIN, validateCode4String,300,TimeUnit.SECONDS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EXCEPION);
        }
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, validateCode4String);
    }
}
